
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
    <meta charset="UTF-8">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>7.3： 跨组件状态共享（Provider） · 《Flutter实战》</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="baidu-site-verification" content="Y34sAqDKUH"/>
    <meta name="theme-color" content="#ffffff">
    <!--<meta name="description" content="">-->
    <meta name="description" content="《Flutter实战》是Flutter中文网（https://flutterchina.club）开源的电子书，旨在帮助开发者系统化的了解并学习Flutter。">
    <meta name="keywords" content="flutter实战,flutter书籍,flutter电子书,flutter中文网 ">
    <meta name="generator" content="GitBook 3.2.3">
    
    <meta name="author" content="wendux 824783146@qq.com">
    
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/style.css">

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-search-pro/search.css">
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-ace/ace.css">
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-splitter/splitter.css">
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-prism/prism.css">
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-donate/plugin.css">
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-fontsettings/website.css">
    
    
    

    

    
        
    
        
    
        
    
        
    
        
    

    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="theme.html" />
    
    
    <link rel="prev" href="inherited_widget.html" />
    

    <style>
        .pull-right > i {
            font-size: 25px;
            color: #777 !important;
            cursor: pointer;

        }

        .pull-right > i:hover {
            color: #777 !important;
        }

        body {
            font-size: 15px !important;
        }

        a[href='../todo.html'], a[href='todo.html'] {
            color: #888 !important;
        }

        .copyright {
            text-align: center;
            margin: 80px 16px 30px 16px;
            padding-top: 16px;
            border-top: #eee 1px solid;
            color: grey;
            font-size: .9em;
        }

        .ad {
            color: white;
            text-align: center;
            background: url(https://img.alicdn.com/tfs/TB1Huu7vMmTBuNjy1XbXXaMrVXa-2880-400.png);
            background-size: cover;
            cursor: pointer;
            margin-bottom: 30px;
            padding: 18px
        }

        .ad:hover {
            opacity: .8;
        }
    </style>
</head>
<body>
<!--<div id="ad" style="display: none">-->
    <!--<div class="ad ad0"-->
         <!--onclick="_track('aliyun','https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=8hhi4dts')">-->
        <!--<div style="font-size: 1.3em; padding-right: 120px; position: relative; top: 12px">阿里云新春特惠</div>-->
        <!--<div style="color:orange; font-size: 13px">限时红包 最高￥<span style="font-size: 40px">1888</span></div>-->
    <!--</div>-->
    <!--<div class="ad ad1"-->
         <!--onclick="_track('aliyun-puhui','https://promotion.aliyun.com/ntms/act/qwbk.html?spm=5176.11533457.1089570.10.74e877e33jtaGy&userCode=8hhi4dts')">-->
        <!--<div style="font-size: 1.5em;">阿里云全民云计算-新春特惠</div>-->
        <!--<div style="color:orange; font-size: 18px;padding-top: 4px;">普惠上云，云服务器1核1G仅需293元/年</div>-->
    <!--</div>-->
<!--</div>-->

<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://flutterchina.club" target="_blank" class="custom-link"><div><img height=20 style='position:relative; top:5px; padding-right:5px ' src='https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/favicon.png'/> Flutter中文网</div></a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../intro.html">
            
                <a href="../intro.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">入门篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    第一章：起步
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../chapter1/mobile_development_intro.html">
            
                <a href="../chapter1/mobile_development_intro.html">
            
                    
                    1.1：移动开发技术简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../chapter1/flutter_intro.html">
            
                <a href="../chapter1/flutter_intro.html">
            
                    
                    1.2：初识Flutter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../chapter1/install_flutter.html">
            
                <a href="../chapter1/install_flutter.html">
            
                    
                    1.3：搭建Flutter开发环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../chapter1/dart.html">
            
                <a href="../chapter1/dart.html">
            
                    
                    1.4：Dart语言简介
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    第二章：第一个Flutter应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="../chapter2/first_flutter_app.html">
            
                <a href="../chapter2/first_flutter_app.html">
            
                    
                    2.1：计数器示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="../chapter2/flutter_router.html">
            
                <a href="../chapter2/flutter_router.html">
            
                    
                    2.2：路由管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3" data-path="../chapter2/flutter_package_mgr.html">
            
                <a href="../chapter2/flutter_package_mgr.html">
            
                    
                    2.3：包管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.4" data-path="../chapter2/flutter_assets_mgr.html">
            
                <a href="../chapter2/flutter_assets_mgr.html">
            
                    
                    2.4：资源管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.5" data-path="../chapter2/flutter_app_debug.html">
            
                <a href="../chapter2/flutter_app_debug.html">
            
                    
                    2.5：调试Flutter APP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.6" data-path="../chapter2/thread_model_and_error_report.html">
            
                <a href="../chapter2/thread_model_and_error_report.html">
            
                    
                    2.6：Dart线程模型及异常捕获
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章：基础组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="../chapter3/flutter_widget_intro.html">
            
                <a href="../chapter3/flutter_widget_intro.html">
            
                    
                    3.1：Widget简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="../chapter3/state_manage.html">
            
                <a href="../chapter3/state_manage.html">
            
                    
                    3.2：状态管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.3" data-path="../chapter3/text.html">
            
                <a href="../chapter3/text.html">
            
                    
                    3.3：文本、字体样式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.4" data-path="../chapter3/buttons.html">
            
                <a href="../chapter3/buttons.html">
            
                    
                    3.4：按钮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.5" data-path="../chapter3/img_and_icon.html">
            
                <a href="../chapter3/img_and_icon.html">
            
                    
                    3.5：图片和Icon
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.6" data-path="../chapter3/radio_and_checkbox.html">
            
                <a href="../chapter3/radio_and_checkbox.html">
            
                    
                    3.6：单选框和复选框
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.7" data-path="../chapter3/input_and_form.html">
            
                <a href="../chapter3/input_and_form.html">
            
                    
                    3.7：输入框和表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.8" data-path="../chapter3/progress.html">
            
                <a href="../chapter3/progress.html">
            
                    
                    3.8：进度指示器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章：布局类组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../chapter4/intro.html">
            
                <a href="../chapter4/intro.html">
            
                    
                    4.1：布局类组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../chapter4/row_and_column.html">
            
                <a href="../chapter4/row_and_column.html">
            
                    
                    4.2：线性布局（Row、Column）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../chapter4/flex.html">
            
                <a href="../chapter4/flex.html">
            
                    
                    4.3：弹性布局（Flex）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.4" data-path="../chapter4/wrap_and_flow.html">
            
                <a href="../chapter4/wrap_and_flow.html">
            
                    
                    4.4：流式布局（Wrap、Flow）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.5" data-path="../chapter4/stack.html">
            
                <a href="../chapter4/stack.html">
            
                    
                    4.5：层叠布局（Stack、Positioned）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.6" data-path="../chapter4/alignment.html">
            
                <a href="../chapter4/alignment.html">
            
                    
                    4.6：对齐与相对定位（Align）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章：容器类组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../chapter5/padding.html">
            
                <a href="../chapter5/padding.html">
            
                    
                    5.1：填充（Padding）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2" data-path="../chapter5/constrainedbox_and_sizebox.html">
            
                <a href="../chapter5/constrainedbox_and_sizebox.html">
            
                    
                    5.2：尺寸限制类容器（ConstrainedBox等）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.3" data-path="../chapter5/decoratedbox.html">
            
                <a href="../chapter5/decoratedbox.html">
            
                    
                    5.3：装饰容器（DecoratedBox）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.4" data-path="../chapter5/transform.html">
            
                <a href="../chapter5/transform.html">
            
                    
                    5.4：变换（Transform）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.5" data-path="../chapter5/container.html">
            
                <a href="../chapter5/container.html">
            
                    
                    5.5：Container容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.6" data-path="../chapter5/material_scaffold.html">
            
                <a href="../chapter5/material_scaffold.html">
            
                    
                    5.6：Scaffold、TabBar、底部导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.7" data-path="../chapter5/clip.html">
            
                <a href="../chapter5/clip.html">
            
                    
                    5.7：剪裁（Clip）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章：可滚动组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="../chapter6/intro.html">
            
                <a href="../chapter6/intro.html">
            
                    
                    6.1：可滚动组件简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.2" data-path="../chapter6/single_child_scrollview.html">
            
                <a href="../chapter6/single_child_scrollview.html">
            
                    
                    6.2：SingleChildScrollView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.3" data-path="../chapter6/listview.html">
            
                <a href="../chapter6/listview.html">
            
                    
                    6.3：ListView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.4" data-path="../chapter6/gridview.html">
            
                <a href="../chapter6/gridview.html">
            
                    
                    6.4：GridView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.5" data-path="../chapter6/custom_scrollview.html">
            
                <a href="../chapter6/custom_scrollview.html">
            
                    
                    6.5：CustomScrollView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.6" data-path="../chapter6/scroll_controller.html">
            
                <a href="../chapter6/scroll_controller.html">
            
                    
                    6.6：滚动监听及控制（ScrollController）
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="./">
            
                <a href="./">
            
                    
                    第七章：功能型组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.7.1" data-path="willpopscope.html">
            
                <a href="willpopscope.html">
            
                    
                    7.1：导航返回拦截（WillPopScope）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.2" data-path="inherited_widget.html">
            
                <a href="inherited_widget.html">
            
                    
                    7.2：数据共享（InheritedWidget）
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.7.3" data-path="provider.html">
            
                <a href="provider.html">
            
                    
                    7.3： 跨组件状态共享（Provider）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.4" data-path="theme.html">
            
                <a href="theme.html">
            
                    
                    7.4：颜色和主题（Theme）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.5" data-path="futurebuilder_and_streambuilder.html">
            
                <a href="futurebuilder_and_streambuilder.html">
            
                    
                    7.5：异步UI更新（FutureBuilder、StreamBuilder）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7.6" data-path="dailog.html">
            
                <a href="dailog.html">
            
                    
                    7.6：对话框详解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">进阶篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../chapter8/">
            
                <a href="../chapter8/">
            
                    
                    第八章：事件处理与通知
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../chapter8/listener.html">
            
                <a href="../chapter8/listener.html">
            
                    
                    8.1：原始指针事件处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../chapter8/gesture.html">
            
                <a href="../chapter8/gesture.html">
            
                    
                    8.2：手势识别
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../chapter8/eventbus.html">
            
                <a href="../chapter8/eventbus.html">
            
                    
                    8.3：全局事件总线
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../chapter8/notification.html">
            
                <a href="../chapter8/notification.html">
            
                    
                    8.4：通知(Notification)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../chapter9/">
            
                <a href="../chapter9/">
            
                    
                    第九章：动画
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="../chapter9/intro.html">
            
                <a href="../chapter9/intro.html">
            
                    
                    9.1：Flutter动画简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="../chapter9/animation_structure.html">
            
                <a href="../chapter9/animation_structure.html">
            
                    
                    9.2：动画结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="../chapter9/route_transition.html">
            
                <a href="../chapter9/route_transition.html">
            
                    
                    9.3：自定义路由过渡动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="../chapter9/hero.html">
            
                <a href="../chapter9/hero.html">
            
                    
                    9.4：Hero动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.5" data-path="../chapter9/stagger_animation.html">
            
                <a href="../chapter9/stagger_animation.html">
            
                    
                    9.5：交织动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.6" data-path="../chapter9/animated_switcher.html">
            
                <a href="../chapter9/animated_switcher.html">
            
                    
                    9.6：通用"切换动画"组件（AnimatedSwitcher）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2.7" data-path="../chapter9/animated_widgets.html">
            
                <a href="../chapter9/animated_widgets.html">
            
                    
                    9.7：动画过渡组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../chapter10/">
            
                <a href="../chapter10/">
            
                    
                    第十章：自定义组件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="../chapter10/intro.html">
            
                <a href="../chapter10/intro.html">
            
                    
                    10.1：自定义组件方法简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="../chapter10/combine.html">
            
                <a href="../chapter10/combine.html">
            
                    
                    10.2：组合现有组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="../chapter10/turn_box.html">
            
                <a href="../chapter10/turn_box.html">
            
                    
                    10.3：组合实例：TurnBox
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="../chapter10/custom_paint.html">
            
                <a href="../chapter10/custom_paint.html">
            
                    
                    10.4：自绘组件（CustomPaint与Canvas）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="../chapter10/gradient_circular_progress_demo.html">
            
                <a href="../chapter10/gradient_circular_progress_demo.html">
            
                    
                    10.5：自绘实例：圆形渐变进度条(自绘)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../chapter11/">
            
                <a href="../chapter11/">
            
                    
                    第十一章：文件操作与网络请求
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="../chapter11/file_operation.html">
            
                <a href="../chapter11/file_operation.html">
            
                    
                    11.1：文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="../chapter11/http.html">
            
                <a href="../chapter11/http.html">
            
                    
                    11.2：Http请求-HttpClient
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="../chapter11/dio.html">
            
                <a href="../chapter11/dio.html">
            
                    
                    11.3：Http请求-Dio package
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="../chapter11/download_with_chunks.html">
            
                <a href="../chapter11/download_with_chunks.html">
            
                    
                    11.4：实例：Http分块下载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="../chapter11/websocket.html">
            
                <a href="../chapter11/websocket.html">
            
                    
                    11.5：WebSocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.6" data-path="../chapter11/socket.html">
            
                <a href="../chapter11/socket.html">
            
                    
                    11.6：使用Socket API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4.7" data-path="../chapter11/json_model.html">
            
                <a href="../chapter11/json_model.html">
            
                    
                    11.7：Json转Dart Model类
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../chapter12/">
            
                <a href="../chapter12/">
            
                    
                    第十二章：包与插件
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="../chapter12/develop_package.html">
            
                <a href="../chapter12/develop_package.html">
            
                    
                    12.1：开发package
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.2" data-path="../chapter12/platform-channel.html">
            
                <a href="../chapter12/platform-channel.html">
            
                    
                    12.2：平台通道简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.3" data-path="../chapter12/develop_plugin.html">
            
                <a href="../chapter12/develop_plugin.html">
            
                    
                    12.3：开发Flutter插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.4" data-path="../chapter12/android_implement.html">
            
                <a href="../chapter12/android_implement.html">
            
                    
                    12.4：插件开发：实现Android端API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.5" data-path="../chapter12/ios_implement.html">
            
                <a href="../chapter12/ios_implement.html">
            
                    
                    12.5：插件开发：实现IOS端API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5.6" data-path="../chapter12/texture_platformview.html">
            
                <a href="../chapter12/texture_platformview.html">
            
                    
                    12.6：Texture和PlatformView
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../chapter13/">
            
                <a href="../chapter13/">
            
                    
                    第十三章：国际化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="../chapter13/multi_languages_support.html">
            
                <a href="../chapter13/multi_languages_support.html">
            
                    
                    13.1：让App支持多语言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.2" data-path="../chapter13/locallization_implement.html">
            
                <a href="../chapter13/locallization_implement.html">
            
                    
                    13.2：实现Localizations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.3" data-path="../chapter13/intl.html">
            
                <a href="../chapter13/intl.html">
            
                    
                    13.3：使用Intl包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6.4" data-path="../chapter13/faq.html">
            
                <a href="../chapter13/faq.html">
            
                    
                    13.4：国际化常见问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../chapter14/">
            
                <a href="../chapter14/">
            
                    
                    第十四章：Flutter核心原理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.7.1" data-path="../chapter14/flutter_ui_system.html">
            
                <a href="../chapter14/flutter_ui_system.html">
            
                    
                    14.1：Flutter UI系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.2" data-path="../chapter14/element_buildcontext.html">
            
                <a href="../chapter14/element_buildcontext.html">
            
                    
                    14.2：Element和BuildContext
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.3" data-path="../chapter14/render_object.html">
            
                <a href="../chapter14/render_object.html">
            
                    
                    14.3：RenderObject与RenderBox
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.4" data-path="../chapter14/flutter_app_startup.html">
            
                <a href="../chapter14/flutter_app_startup.html">
            
                    
                    14.4：Flutter从启动到显示
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7.5" data-path="../chapter14/image_and_cache.html">
            
                <a href="../chapter14/image_and_cache.html">
            
                    
                    14.5：Flutter图片加载与缓存
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">实例篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../chapter15/intro.html">
            
                <a href="../chapter15/intro.html">
            
                    
                    第十五章：一个完整的Flutter应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../chapter15/intro.html">
            
                <a href="../chapter15/intro.html">
            
                    
                    15.1：应用简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../chapter15/code_structure.html">
            
                <a href="../chapter15/code_structure.html">
            
                    
                    15.2：APP代码结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="../chapter15/models.html">
            
                <a href="../chapter15/models.html">
            
                    
                    15.3：Model类定义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="../chapter15/globals.html">
            
                <a href="../chapter15/globals.html">
            
                    
                    15.4：全局变量及共享状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="../chapter15/network.html">
            
                <a href="../chapter15/network.html">
            
                    
                    15.5：网络请求封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="../chapter15/entry.html">
            
                <a href="../chapter15/entry.html">
            
                    
                    15.6：App入口及首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="../chapter15/login_page.html">
            
                <a href="../chapter15/login_page.html">
            
                    
                    15.7：登录页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.8" data-path="../chapter15/language_and_theme_setting.html">
            
                <a href="../chapter15/language_and_theme_setting.html">
            
                    
                    15.8：多语言和多主题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    
    <a class="btn pull-right" aria-label="GitHub">
        <i class="fa fa-edit" title="编辑本文"></i>
    </a>
    <a class="btn pull-right" aria-label="GitHub" href="https://github.com/flutterchina/flutter-in-action">
        <i class="fa fa-github" title="查看本书开源项目"></i>
    </a>
    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >7.3： 跨组件状态共享（Provider）</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="73-&#x8DE8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;&#x5171;&#x4EAB;&#xFF08;provider&#xFF09;">7.3 &#x8DE8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;&#x5171;&#x4EAB;&#xFF08;Provider&#xFF09;</h1>
<p>&#x5728;Flutter&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x662F;&#x4E00;&#x4E2A;&#x6C38;&#x6052;&#x7684;&#x8BDD;&#x9898;&#x3002;&#x4E00;&#x822C;&#x7684;&#x539F;&#x5219;&#x662F;&#xFF1A;&#x5982;&#x679C;&#x72B6;&#x6001;&#x662F;&#x7EC4;&#x4EF6;&#x79C1;&#x6709;&#x7684;&#xFF0C;&#x5219;&#x5E94;&#x8BE5;&#x7531;&#x7EC4;&#x4EF6;&#x81EA;&#x5DF1;&#x7BA1;&#x7406;&#xFF1B;&#x5982;&#x679C;&#x72B6;&#x6001;&#x8981;&#x8DE8;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#xFF0C;&#x5219;&#x8BE5;&#x72B6;&#x6001;&#x5E94;&#x8BE5;&#x7531;&#x5404;&#x4E2A;&#x7EC4;&#x4EF6;&#x5171;&#x540C;&#x7684;&#x7236;&#x5143;&#x7D20;&#x6765;&#x7BA1;&#x7406;&#x3002;&#x5BF9;&#x4E8E;&#x7EC4;&#x4EF6;&#x79C1;&#x6709;&#x7684;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x5F88;&#x597D;&#x7406;&#x89E3;&#xFF0C;&#x4F46;&#x5BF9;&#x4E8E;&#x8DE8;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x7BA1;&#x7406;&#x7684;&#x65B9;&#x5F0F;&#x5C31;&#x6BD4;&#x8F83;&#x591A;&#x4E86;&#xFF0C;&#x5982;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x4E8B;&#x4EF6;&#x603B;&#x7EBF;EventBus&#xFF08;&#x5C06;&#x5728;&#x4E0B;&#x4E00;&#x7AE0;&#x4E2D;&#x4ECB;&#x7ECD;&#xFF09;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x901A;&#x8FC7;&#x5B83;&#x5C31;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x8DE8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;&#x540C;&#x6B65;&#xFF1A;&#x72B6;&#x6001;&#x6301;&#x6709;&#x65B9;&#xFF08;&#x53D1;&#x5E03;&#x8005;&#xFF09;&#x8D1F;&#x8D23;&#x66F4;&#x65B0;&#x3001;&#x53D1;&#x5E03;&#x72B6;&#x6001;&#xFF0C;&#x72B6;&#x6001;&#x4F7F;&#x7528;&#x65B9;&#xFF08;&#x89C2;&#x5BDF;&#x8005;&#xFF09;&#x76D1;&#x542C;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x4E8B;&#x4EF6;&#x6765;&#x6267;&#x884C;&#x4E00;&#x4E9B;&#x64CD;&#x4F5C;&#x3002;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E2A;&#x767B;&#x9646;&#x72B6;&#x6001;&#x540C;&#x6B65;&#x7684;&#x7B80;&#x5355;&#x793A;&#x4F8B;&#xFF1A;</p>
<p>&#x5B9A;&#x4E49;&#x4E8B;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">enum</span> Event<span class="token punctuation">{</span>
  login<span class="token punctuation">,</span>
  <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token comment">//&#x7701;&#x7565;&#x5176;&#x5B83;&#x4E8B;&#x4EF6;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x767B;&#x5F55;&#x9875;&#x4EE3;&#x7801;&#x5927;&#x81F4;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token comment">// &#x767B;&#x5F55;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x540E;&#x53D1;&#x5E03;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x4E8B;&#x4EF6;</span>
bus<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span>Event<span class="token punctuation">.</span>login<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x4F9D;&#x8D56;&#x767B;&#x5F55;&#x72B6;&#x6001;&#x7684;&#x9875;&#x9762;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">void</span> <span class="token function">onLoginChanged</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token comment">//&#x767B;&#x5F55;&#x72B6;&#x6001;&#x53D8;&#x5316;&#x5904;&#x7406;&#x903B;&#x8F91;</span>
<span class="token punctuation">}</span>

<span class="token metadata symbol">@override</span>
<span class="token keyword">void</span> <span class="token function">initState</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">//&#x8BA2;&#x9605;&#x767B;&#x5F55;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x4E8B;&#x4EF6;</span>
  bus<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span>Event<span class="token punctuation">.</span>login<span class="token punctuation">,</span>onLogin<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">initState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token metadata symbol">@override</span>
<span class="token keyword">void</span> <span class="token function">dispose</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">//&#x53D6;&#x6D88;&#x8BA2;&#x9605;</span>
  bus<span class="token punctuation">.</span><span class="token function">off</span><span class="token punctuation">(</span>Event<span class="token punctuation">.</span>login<span class="token punctuation">,</span>onLogin<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">dispose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#xFF0C;&#x901A;&#x8FC7;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#x6765;&#x5B9E;&#x73B0;&#x8DE8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;&#x5171;&#x4EAB;&#x6709;&#x4E00;&#x4E9B;&#x660E;&#x663E;&#x7684;&#x7F3A;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x5FC5;&#x987B;&#x663E;&#x5F0F;&#x5B9A;&#x4E49;&#x5404;&#x79CD;&#x4E8B;&#x4EF6;&#xFF0C;&#x4E0D;&#x597D;&#x7BA1;&#x7406;</li>
<li>&#x8BA2;&#x9605;&#x8005;&#x5FC5;&#x987B;&#x9700;&#x663E;&#x5F0F;&#x6CE8;&#x518C;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x56DE;&#x8C03;&#xFF0C;&#x4E5F;&#x5FC5;&#x987B;&#x5728;&#x7EC4;&#x4EF6;&#x9500;&#x6BC1;&#x65F6;&#x624B;&#x52A8;&#x53BB;&#x89E3;&#x7ED1;&#x56DE;&#x8C03;&#x4EE5;&#x907F;&#x514D;&#x5185;&#x5B58;&#x6CC4;&#x9732;&#x3002;</li>
</ol>
<p>&#x5728;Flutter&#x5F53;&#x4E2D;&#x6709;&#x6CA1;&#x6709;&#x66F4;&#x597D;&#x7684;&#x8DE8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x65B9;&#x5F0F;&#x4E86;&#x5462;&#xFF1F;&#x7B54;&#x6848;&#x662F;&#x80AF;&#x5B9A;&#x7684;&#xFF0C;&#x90A3;&#x600E;&#x4E48;&#x505A;&#x7684;&#xFF1F;&#x6211;&#x4EEC;&#x60F3;&#x60F3;&#x524D;&#x9762;&#x4ECB;&#x7ECD;&#x7684;<code>InheritedWidget</code>&#xFF0C;&#x5B83;&#x7684;&#x5929;&#x751F;&#x7279;&#x6027;&#x5C31;&#x662F;&#x80FD;&#x7ED1;&#x5B9A;<code>InheritedWidget</code>&#x4E0E;&#x4F9D;&#x8D56;&#x5B83;&#x7684;&#x5B50;&#x5B59;&#x7EC4;&#x4EF6;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x5E76;&#x4E14;&#x5F53;<code>InheritedWidget</code>&#x6570;&#x636E;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x66F4;&#x65B0;&#x4F9D;&#x8D56;&#x7684;&#x5B50;&#x5B59;&#x7EC4;&#x4EF6;&#xFF01;&#x5229;&#x7528;&#x8FD9;&#x4E2A;&#x7279;&#x6027;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;&#x9700;&#x8981;&#x8DE8;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#x7684;&#x72B6;&#x6001;&#x4FDD;&#x5B58;&#x5728;<code>InheritedWidget</code>&#x4E2D;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x5B50;&#x7EC4;&#x4EF6;&#x4E2D;&#x5F15;&#x7528;<code>InheritedWidget</code>&#x5373;&#x53EF;&#xFF0C;Flutter&#x793E;&#x533A;&#x8457;&#x540D;&#x7684;Provider&#x5305;&#x6B63;&#x662F;&#x57FA;&#x4E8E;&#x8FD9;&#x4E2A;&#x601D;&#x60F3;&#x5B9E;&#x73B0;&#x7684;&#x4E00;&#x5957;&#x8DE8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;&#x5171;&#x4EAB;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x4FBF;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;Provider&#x7684;&#x7528;&#x6CD5;&#x53CA;&#x539F;&#x7406;&#x3002;</p>
<h2 id="provider">Provider</h2>
<p>&#x4E3A;&#x4E86;&#x52A0;&#x5F3A;&#x8BFB;&#x8005;&#x7684;&#x7406;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x76F4;&#x63A5;&#x53BB;&#x770B;Provider&#x5305;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#xFF0C;&#x76F8;&#x53CD;&#xFF0C;&#x6211;&#x4F1A;&#x5E26;&#x7740;&#x4F60;&#x6839;&#x636E;&#x4E0A;&#x9762;&#x63CF;&#x8FF0;&#x7684;&#x901A;&#x8FC7;<code>InheritedWidget</code>&#x5B9E;&#x73B0;&#x7684;&#x601D;&#x8DEF;&#x6765;&#x4E00;&#x6B65;&#x4E00;&#x6B65;&#x5730;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x6700;&#x5C0F;&#x529F;&#x80FD;&#x7684;Provider&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x4FDD;&#x5B58;&#x9700;&#x8981;&#x5171;&#x4EAB;&#x7684;&#x6570;&#x636E;<code>InheritedWidget</code>&#xFF0C;&#x7531;&#x4E8E;&#x5177;&#x4F53;&#x4E1A;&#x52A1;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x4E0D;&#x53EF;&#x9884;&#x671F;&#xFF0C;&#x4E3A;&#x4E86;&#x901A;&#x7528;&#x6027;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x6CDB;&#x578B;&#xFF0C;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x901A;&#x7528;&#x7684;<code>InheritedProvider</code>&#x7C7B;&#xFF0C;&#x5B83;&#x7EE7;&#x627F;&#x81EA;<code>InheritedWidget</code>&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token comment">// &#x4E00;&#x4E2A;&#x901A;&#x7528;&#x7684;InheritedWidget&#xFF0C;&#x4FDD;&#x5B58;&#x4EFB;&#x9700;&#x8981;&#x8DE8;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#x7684;&#x72B6;&#x6001;</span>
<span class="token keyword">class</span> <span class="token class-name">InheritedProvider</span><span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token keyword">extends</span> <span class="token class-name">InheritedWidget</span> <span class="token punctuation">{</span>
  <span class="token function">InheritedProvider</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token metadata symbol">@required</span> <span class="token keyword">this</span><span class="token punctuation">.</span>data<span class="token punctuation">,</span> Widget child<span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword">super</span><span class="token punctuation">(</span>child<span class="token punctuation">:</span> child<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">//&#x5171;&#x4EAB;&#x72B6;&#x6001;&#x4F7F;&#x7528;&#x6CDB;&#x578B;</span>
  <span class="token keyword">final</span> T data<span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  bool <span class="token function">updateShouldNotify</span><span class="token punctuation">(</span>InheritedProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> old<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x5728;&#x6B64;&#x7B80;&#x5355;&#x8FD4;&#x56DE;true&#xFF0C;&#x5219;&#x6BCF;&#x6B21;&#x66F4;&#x65B0;&#x90FD;&#x4F1A;&#x8C03;&#x7528;&#x4F9D;&#x8D56;&#x5176;&#x7684;&#x5B50;&#x5B59;&#x8282;&#x70B9;&#x7684;`didChangeDependencies`&#x3002;</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6570;&#x636E;&#x4FDD;&#x5B58;&#x7684;&#x5730;&#x65B9;&#x6709;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x505A;&#x7684;&#x5C31;&#x662F;&#x5728;&#x6570;&#x636E;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x7684;&#x65F6;&#x5019;&#x6765;&#x91CD;&#x65B0;&#x6784;&#x5EFA;<code>InheritedProvider</code>&#xFF0C;&#x90A3;&#x4E48;&#x73B0;&#x5728;&#x5C31;&#x9762;&#x4E34;&#x4E24;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;</p>
<ol>
<li>&#x6570;&#x636E;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x600E;&#x4E48;&#x901A;&#x77E5;&#xFF1F;</li>
<li>&#x8C01;&#x6765;&#x91CD;&#x65B0;&#x6784;&#x5EFA;<code>InheritedProvider</code>&#xFF1F;</li>
</ol>
<p>&#x7B2C;&#x4E00;&#x4E2A;&#x95EE;&#x9898;&#x5176;&#x5B9E;&#x5F88;&#x597D;&#x89E3;&#x51B3;&#xFF0C;&#x6211;&#x4EEC;&#x5F53;&#x7136;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x4ECB;&#x7ECD;&#x7684;eventBus&#x6765;&#x8FDB;&#x884C;&#x4E8B;&#x4EF6;&#x901A;&#x77E5;&#xFF0C;&#x4F46;&#x662F;&#x4E3A;&#x4E86;&#x66F4;&#x8D34;&#x8FD1;Flutter&#x5F00;&#x53D1;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;Flutter&#x4E2D;SDK&#x4E2D;&#x63D0;&#x4F9B;&#x7684;<code>ChangeNotifier</code>&#x7C7B; &#xFF0C;&#x5B83;&#x7EE7;&#x627F;&#x81EA;<code>Listenable</code>&#xFF0C;&#x4E5F;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;Flutter&#x98CE;&#x683C;&#x7684;&#x53D1;&#x5E03;&#x8005;-&#x8BA2;&#x9605;&#x8005;&#x6A21;&#x5F0F;&#xFF0C;<code>ChangeNotifier</code>&#x5B9A;&#x4E49;&#x5927;&#x81F4;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">ChangeNotifier</span> <span class="token keyword">implements</span> <span class="token class-name">Listenable</span> <span class="token punctuation">{</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">addListener</span><span class="token punctuation">(</span>VoidCallback listener<span class="token punctuation">)</span> <span class="token punctuation">{</span>
     <span class="token comment">//&#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x5668;</span>
  <span class="token punctuation">}</span>
  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">removeListener</span><span class="token punctuation">(</span>VoidCallback listener<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x79FB;&#x9664;&#x76D1;&#x542C;&#x5668;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">void</span> <span class="token function">notifyListeners</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x901A;&#x77E5;&#x6240;&#x6709;&#x76D1;&#x542C;&#x5668;&#xFF0C;&#x89E6;&#x53D1;&#x76D1;&#x542C;&#x5668;&#x56DE;&#x8C03;   </span>
  <span class="token punctuation">}</span>

  <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token comment">//&#x7701;&#x7565;&#x65E0;&#x5173;&#x4EE3;&#x7801;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8C03;&#x7528;<code>addListener()</code>&#x548C;<code>removeListener()</code>&#x6765;&#x6DFB;&#x52A0;&#x3001;&#x79FB;&#x9664;&#x76D1;&#x542C;&#x5668;&#xFF08;&#x8BA2;&#x9605;&#x8005;&#xFF09;&#xFF1B;&#x901A;&#x8FC7;&#x8C03;&#x7528;<code>notifyListeners()</code> &#x53EF;&#x4EE5;&#x89E6;&#x53D1;&#x6240;&#x6709;&#x76D1;&#x542C;&#x5668;&#x56DE;&#x8C03;&#x3002;</p>
<p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x8981;&#x5171;&#x4EAB;&#x7684;&#x72B6;&#x6001;&#x653E;&#x5230;&#x4E00;&#x4E2A;Model&#x7C7B;&#x4E2D;&#xFF0C;&#x7136;&#x540E;&#x8BA9;&#x5B83;&#x7EE7;&#x627F;&#x81EA;<code>ChangeNotifier</code>&#xFF0C;&#x8FD9;&#x6837;&#x5F53;&#x5171;&#x4EAB;&#x7684;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x8C03;&#x7528;<code>notifyListeners()</code> &#x6765;&#x901A;&#x77E5;&#x8BA2;&#x9605;&#x8005;&#xFF0C;&#x7136;&#x540E;&#x7531;&#x8BA2;&#x9605;&#x8005;&#x6765;&#x91CD;&#x65B0;&#x6784;&#x5EFA;<code>InheritedProvider</code>&#xFF0C;&#x8FD9;&#x4E5F;&#x662F;&#x7B2C;&#x4E8C;&#x4E2A;&#x95EE;&#x9898;&#x7684;&#x7B54;&#x6848;&#xFF01;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x4FBF;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x8BA2;&#x9605;&#x8005;&#x7C7B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token comment">// &#x8BE5;&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5728;Dart&#x4E2D;&#x83B7;&#x53D6;&#x6A21;&#x677F;&#x7C7B;&#x578B;</span>
Type _typeOf<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> T<span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">ChangeNotifierProvider</span><span class="token operator">&lt;</span>T <span class="token keyword">extends</span> <span class="token class-name">ChangeNotifier</span><span class="token operator">&gt;</span> <span class="token keyword">extends</span> <span class="token class-name">StatefulWidget</span> <span class="token punctuation">{</span>
  <span class="token function">ChangeNotifierProvider</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    Key key<span class="token punctuation">,</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>data<span class="token punctuation">,</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>child<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">final</span> Widget child<span class="token punctuation">;</span>
  <span class="token keyword">final</span> T data<span class="token punctuation">;</span>

  <span class="token comment">//&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4FBF;&#x6377;&#x65B9;&#x6CD5;&#xFF0C;&#x65B9;&#x4FBF;&#x5B50;&#x6811;&#x4E2D;&#x7684;widget&#x83B7;&#x53D6;&#x5171;&#x4EAB;&#x6570;&#x636E;</span>
  <span class="token keyword">static</span> T of<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">final</span> type <span class="token operator">=</span> _typeOf<span class="token operator">&lt;</span>InheritedProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">final</span> provider <span class="token operator">=</span>  context<span class="token punctuation">.</span><span class="token function">inheritFromWidgetOfExactType</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span> <span class="token operator">as</span> InheritedProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span>&#xFF1B;
    <span class="token keyword">return</span> provider<span class="token punctuation">.</span>data<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  _ChangeNotifierProviderState<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token function">createState</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> _ChangeNotifierProviderState<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8BE5;&#x7C7B;&#x7EE7;&#x627F;<code>StatefulWidget</code>&#xFF0C;&#x7136;&#x540E;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;<code>of()</code>&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x4F9B;&#x5B50;&#x7C7B;&#x65B9;&#x4FBF;&#x83B7;&#x53D6;Widget&#x6811;&#x4E2D;&#x7684;<code>InheritedProvider</code>&#x4E2D;&#x4FDD;&#x5B58;&#x7684;&#x5171;&#x4EAB;&#x72B6;&#x6001;(model)&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x8BE5;&#x7C7B;&#x5BF9;&#x5E94;&#x7684;<code>_ChangeNotifierProviderState</code>&#x7C7B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">_ChangeNotifierProviderState</span><span class="token operator">&lt;</span>T <span class="token keyword">extends</span> <span class="token class-name">ChangeNotifier</span><span class="token operator">&gt;</span> <span class="token keyword">extends</span> <span class="token class-name">State</span><span class="token operator">&lt;</span>ChangeNotifierProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x5982;&#x679C;&#x6570;&#x636E;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF08;model&#x7C7B;&#x8C03;&#x7528;&#x4E86;notifyListeners&#xFF09;&#xFF0C;&#x91CD;&#x65B0;&#x6784;&#x5EFA;InheritedProvider</span>
    <span class="token function">setState</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">didUpdateWidget</span><span class="token punctuation">(</span>ChangeNotifierProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> oldWidget<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x5F53;Provider&#x66F4;&#x65B0;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x65B0;&#x65E7;&#x6570;&#x636E;&#x4E0D;&quot;==&quot;&#xFF0C;&#x5219;&#x89E3;&#x7ED1;&#x65E7;&#x6570;&#x636E;&#x76D1;&#x542C;&#xFF0C;&#x540C;&#x65F6;&#x6DFB;&#x52A0;&#x65B0;&#x6570;&#x636E;&#x76D1;&#x542C;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>widget<span class="token punctuation">.</span>data <span class="token operator">!=</span> oldWidget<span class="token punctuation">.</span>data<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      oldWidget<span class="token punctuation">.</span>data<span class="token punctuation">.</span><span class="token function">removeListener</span><span class="token punctuation">(</span>update<span class="token punctuation">)</span><span class="token punctuation">;</span>
      widget<span class="token punctuation">.</span>data<span class="token punctuation">.</span><span class="token function">addListener</span><span class="token punctuation">(</span>update<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">didUpdateWidget</span><span class="token punctuation">(</span>oldWidget<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">initState</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x7ED9;model&#x6DFB;&#x52A0;&#x76D1;&#x542C;&#x5668;</span>
    widget<span class="token punctuation">.</span>data<span class="token punctuation">.</span><span class="token function">addListener</span><span class="token punctuation">(</span>update<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">initState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  <span class="token keyword">void</span> <span class="token function">dispose</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x79FB;&#x9664;model&#x7684;&#x76D1;&#x542C;&#x5668;</span>
    widget<span class="token punctuation">.</span>data<span class="token punctuation">.</span><span class="token function">removeListener</span><span class="token punctuation">(</span>update<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">dispose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> InheritedProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">(</span>
      data<span class="token punctuation">:</span> widget<span class="token punctuation">.</span>data<span class="token punctuation">,</span>
      child<span class="token punctuation">:</span> widget<span class="token punctuation">.</span>child<span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;<code>_ChangeNotifierProviderState</code>&#x7C7B;&#x7684;&#x4E3B;&#x8981;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x76D1;&#x542C;&#x5230;&#x5171;&#x4EAB;&#x72B6;&#x6001;&#xFF08;model&#xFF09;&#x6539;&#x53D8;&#x65F6;&#x91CD;&#x65B0;&#x6784;&#x5EFA;Widget&#x6811;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x5728;<code>_ChangeNotifierProviderState</code>&#x7C7B;&#x4E2D;&#x8C03;&#x7528;<code>setState()</code>&#x65B9;&#x6CD5;&#xFF0C;<code>widget.child</code>&#x59CB;&#x7EC8;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#xFF0C;&#x6240;&#x4EE5;&#x6267;&#x884C;build&#x65F6;&#xFF0C;<code>InheritedProvider</code>&#x7684;child&#x5F15;&#x7528;&#x7684;&#x59CB;&#x7EC8;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5B50;widget&#xFF0C;&#x6240;&#x4EE5;<code>widget.child</code>&#x5E76;&#x4E0D;&#x4F1A;&#x91CD;&#x65B0;<code>build</code>&#xFF0C;&#x8FD9;&#x4E5F;&#x5C31;&#x76F8;&#x5F53;&#x4E8E;&#x5BF9;<code>child</code>&#x8FDB;&#x884C;&#x4E86;&#x7F13;&#x5B58;&#xFF01;&#x5F53;&#x7136;&#x5982;&#x679C;<code>ChangeNotifierProvider</code>&#x7236;&#x7EA7;Widget&#x91CD;&#x65B0;build&#x65F6;&#xFF0C;&#x5219;&#x5176;&#x4F20;&#x5165;&#x7684;<code>child</code>&#x4FBF;&#x6709;&#x53EF;&#x80FD;&#x4F1A;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6240;&#x9700;&#x8981;&#x7684;&#x5404;&#x4E2A;&#x5DE5;&#x5177;&#x7C7B;&#x90FD;&#x5DF2;&#x5B8C;&#x6210;&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x8D2D;&#x7269;&#x8F66;&#x7684;&#x4F8B;&#x5B50;&#x6765;&#x770B;&#x770B;&#x600E;&#x4E48;&#x4F7F;&#x7528;&#x4E0A;&#x9762;&#x7684;&#x8FD9;&#x4E9B;&#x7C7B;&#x3002;</p>
<h3 id="&#x8D2D;&#x7269;&#x8F66;&#x793A;&#x4F8B;">&#x8D2D;&#x7269;&#x8F66;&#x793A;&#x4F8B;</h3>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x663E;&#x793A;&#x8D2D;&#x7269;&#x8F66;&#x4E2D;&#x6240;&#x6709;&#x5546;&#x54C1;&#x603B;&#x4EF7;&#x7684;&#x529F;&#x80FD;&#xFF1A;</p>
<ol>
<li>&#x5411;&#x8D2D;&#x7269;&#x8F66;&#x4E2D;&#x6DFB;&#x52A0;&#x65B0;&#x5546;&#x54C1;&#x65F6;&#x603B;&#x4EF7;&#x66F4;&#x65B0;</li>
</ol>
<p>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;<code>Item</code>&#x7C7B;&#xFF0C;&#x7528;&#x4E8E;&#x8868;&#x793A;&#x5546;&#x54C1;&#x4FE1;&#x606F;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">Item</span> <span class="token punctuation">{</span>
  <span class="token function">Item</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>price<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>
  double price<span class="token punctuation">;</span> <span class="token comment">//&#x5546;&#x54C1;&#x5355;&#x4EF7;</span>
  int count<span class="token punctuation">;</span> <span class="token comment">// &#x5546;&#x54C1;&#x4EFD;&#x6570;</span>
  <span class="token comment">//... &#x7701;&#x7565;&#x5176;&#x5B83;&#x5C5E;&#x6027;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4FDD;&#x5B58;&#x8D2D;&#x7269;&#x8F66;&#x5185;&#x5546;&#x54C1;&#x6570;&#x636E;&#x7684;<code>CartModel</code>&#x7C7B;:</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">CartModel</span> <span class="token keyword">extends</span> <span class="token class-name">ChangeNotifier</span> <span class="token punctuation">{</span>
  <span class="token comment">// &#x7528;&#x4E8E;&#x4FDD;&#x5B58;&#x8D2D;&#x7269;&#x8F66;&#x4E2D;&#x5546;&#x54C1;&#x5217;&#x8868;</span>
  <span class="token keyword">final</span> List<span class="token operator">&lt;</span>Item<span class="token operator">&gt;</span> _items <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

  <span class="token comment">// &#x7981;&#x6B62;&#x6539;&#x53D8;&#x8D2D;&#x7269;&#x8F66;&#x91CC;&#x7684;&#x5546;&#x54C1;&#x4FE1;&#x606F;</span>
  UnmodifiableListView<span class="token operator">&lt;</span>Item<span class="token operator">&gt;</span> <span class="token keyword">get</span> items <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">UnmodifiableListView</span><span class="token punctuation">(</span>_items<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// &#x8D2D;&#x7269;&#x8F66;&#x4E2D;&#x5546;&#x54C1;&#x7684;&#x603B;&#x4EF7;</span>
  double <span class="token keyword">get</span> totalPrice <span class="token operator">=</span><span class="token operator">&gt;</span>
      _items<span class="token punctuation">.</span><span class="token function">fold</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>value<span class="token punctuation">,</span> item<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> value <span class="token operator">+</span> item<span class="token punctuation">.</span>count <span class="token operator">*</span> item<span class="token punctuation">.</span>price<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// &#x5C06; [item] &#x6DFB;&#x52A0;&#x5230;&#x8D2D;&#x7269;&#x8F66;&#x3002;&#x8FD9;&#x662F;&#x552F;&#x4E00;&#x4E00;&#x79CD;&#x80FD;&#x4ECE;&#x5916;&#x90E8;&#x6539;&#x53D8;&#x8D2D;&#x7269;&#x8F66;&#x7684;&#x65B9;&#x6CD5;&#x3002;</span>
  <span class="token keyword">void</span> <span class="token function">add</span><span class="token punctuation">(</span>Item item<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    _items<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x901A;&#x77E5;&#x76D1;&#x542C;&#x5668;&#xFF08;&#x8BA2;&#x9605;&#x8005;&#xFF09;&#xFF0C;&#x91CD;&#x65B0;&#x6784;&#x5EFA;InheritedProvider&#xFF0C; &#x66F4;&#x65B0;&#x72B6;&#x6001;&#x3002;</span>
    <span class="token function">notifyListeners</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p> <code>CartModel</code>&#x5373;&#x8981;&#x8DE8;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#x7684;model&#x7C7B;&#x3002;&#x6700;&#x540E;&#x6211;&#x4EEC;&#x6784;&#x5EFA;&#x793A;&#x4F8B;&#x9875;&#x9762;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token keyword">class</span> <span class="token class-name">ProviderRoute</span> <span class="token keyword">extends</span> <span class="token class-name">StatefulWidget</span> <span class="token punctuation">{</span>
  <span class="token metadata symbol">@override</span>
  _ProviderRouteState <span class="token function">createState</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">_ProviderRouteState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">_ProviderRouteState</span> <span class="token keyword">extends</span> <span class="token class-name">State</span><span class="token operator">&lt;</span>ProviderRoute<span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">Center</span><span class="token punctuation">(</span>
      child<span class="token punctuation">:</span> ChangeNotifierProvider<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>
        data<span class="token punctuation">:</span> <span class="token function">CartModel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        child<span class="token punctuation">:</span> <span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
          <span class="token keyword">return</span> <span class="token function">Column</span><span class="token punctuation">(</span>
            children<span class="token punctuation">:</span> <span class="token operator">&lt;</span>Widget<span class="token operator">&gt;</span><span class="token punctuation">[</span>
              <span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">var</span> cart<span class="token operator">=</span>ChangeNotifierProvider<span class="token punctuation">.</span>of<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x603B;&#x4EF7;: ${cart.totalPrice}&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
              <span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;RaisedButton build&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//&#x5728;&#x540E;&#x9762;&#x4F18;&#x5316;&#x90E8;&#x5206;&#x4F1A;&#x7528;&#x5230;</span>
                <span class="token keyword">return</span> <span class="token function">RaisedButton</span><span class="token punctuation">(</span>
                  child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                  onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token comment">//&#x7ED9;&#x8D2D;&#x7269;&#x8F66;&#x4E2D;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&#xFF0C;&#x6DFB;&#x52A0;&#x540E;&#x603B;&#x4EF7;&#x4F1A;&#x66F4;&#x65B0;</span>
                    ChangeNotifierProvider<span class="token punctuation">.</span>of<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token function">Item</span><span class="token punctuation">(</span><span class="token number">20.0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                  <span class="token punctuation">}</span><span class="token punctuation">,</span>
                <span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
          <span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x793A;&#x4F8B;&#x540E;&#x6548;&#x679C;&#x5982;&#x56FE;7-2&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/docs/imgs/7-2.png" alt="provider"></p>
<p>&#x6BCF;&#x6B21;&#x70B9;&#x51FB;&#x201D;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&#x201C;&#x6309;&#x94AE;&#xFF0C;&#x603B;&#x4EF7;&#x5C31;&#x4F1A;&#x589E;&#x52A0;20&#xFF0C;&#x6211;&#x4EEC;&#x671F;&#x671B;&#x7684;&#x529F;&#x80FD;&#x5B9E;&#x73B0;&#x4E86;&#xFF01;&#x53EF;&#x80FD;&#x6709;&#x4E9B;&#x8BFB;&#x8005;&#x4F1A;&#x7591;&#x60D1;&#xFF0C;&#x6211;&#x4EEC;&#x9976;&#x4E86;&#x4E00;&#x5927;&#x5708;&#x5B9E;&#x73B0;&#x8FD9;&#x4E48;&#x7B80;&#x5355;&#x7684;&#x529F;&#x80FD;&#x6709;&#x610F;&#x4E49;&#x4E48;&#xFF1F;&#x5176;&#x5B9E;&#xFF0C;&#x5C31;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x6765;&#x770B;&#xFF0C;&#x53EA;&#x662F;&#x66F4;&#x65B0;&#x540C;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x9875;&#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;<code>ChangeNotifierProvider</code>&#x7684;&#x4F18;&#x52BF;&#x5E76;&#x4E0D;&#x660E;&#x663E;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x662F;&#x505A;&#x4E00;&#x4E2A;&#x8D2D;&#x7269;APP&#x5462;&#xFF1F;&#x7531;&#x4E8E;&#x8D2D;&#x7269;&#x8F66;&#x6570;&#x636E;&#x662F;&#x901A;&#x5E38;&#x662F;&#x4F1A;&#x5728;&#x6574;&#x4E2A;APP&#x4E2D;&#x5171;&#x4EAB;&#x7684;&#xFF0C;&#x6BD4;&#x5982;&#x4F1A;&#x8DE8;&#x8DEF;&#x7531;&#x5171;&#x4EAB;&#x3002;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5C06;<code>ChangeNotifierProvider</code>&#x653E;&#x5728;&#x6574;&#x4E2A;&#x5E94;&#x7528;&#x7684;Widget&#x6811;&#x7684;&#x6839;&#x4E0A;&#xFF0C;&#x90A3;&#x4E48;&#x6574;&#x4E2A;APP&#x5C31;&#x53EF;&#x4EE5;&#x5171;&#x4EAB;&#x8D2D;&#x7269;&#x8F66;&#x7684;&#x6570;&#x636E;&#x4E86;&#xFF0C;&#x8FD9;&#x65F6;<code>ChangeNotifierProvider</code>&#x7684;&#x4F18;&#x52BF;&#x5C06;&#x4F1A;&#x975E;&#x5E38;&#x660E;&#x663E;&#x3002;</p>
<p>&#x867D;&#x7136;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x4F46;&#x5B83;&#x5374;&#x5C06;Provider&#x7684;&#x539F;&#x7406;&#x548C;&#x6D41;&#x7A0B;&#x4F53;&#x73B0;&#x7684;&#x5F88;&#x6E05;&#x695A;&#xFF0C;&#x56FE;7-3&#x662F;Provider&#x7684;&#x539F;&#x7406;&#x56FE;&#xFF1A;</p>
<p><img src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/docs/imgs/7-3.png" alt="&#x56FE;7-3"></p>
<p>Model&#x53D8;&#x5316;&#x540E;&#x4F1A;&#x81EA;&#x52A8;&#x901A;&#x77E5;<code>ChangeNotifierProvider</code>&#xFF08;&#x8BA2;&#x9605;&#x8005;&#xFF09;&#xFF0C;<code>ChangeNotifierProvider</code>&#x5185;&#x90E8;&#x4F1A;&#x91CD;&#x65B0;&#x6784;&#x5EFA;<code>InheritedWidget</code>&#xFF0C;&#x800C;&#x4F9D;&#x8D56;&#x8BE5;<code>InheritedWidget</code>&#x7684;&#x5B50;&#x5B59;Widget&#x5C31;&#x4F1A;&#x66F4;&#x65B0;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#x4F7F;&#x7528;Provider&#xFF0C;&#x5C06;&#x4F1A;&#x5E26;&#x6765;&#x5982;&#x4E0B;&#x6536;&#x76CA;&#xFF1A;</p>
<ol>
<li>&#x6211;&#x4EEC;&#x7684;&#x4E1A;&#x52A1;&#x4EE3;&#x7801;&#x66F4;&#x5173;&#x6CE8;&#x6570;&#x636E;&#x4E86;&#xFF0C;&#x53EA;&#x8981;&#x66F4;&#x65B0;Model&#xFF0C;&#x5219;UI&#x4F1A;&#x81EA;&#x52A8;&#x66F4;&#x65B0;&#xFF0C;&#x800C;&#x4E0D;&#x7528;&#x5728;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x540E;&#x518D;&#x53BB;&#x624B;&#x52A8;&#x8C03;&#x7528;<code>setState()</code>&#x6765;&#x663E;&#x5F0F;&#x66F4;&#x65B0;&#x9875;&#x9762;&#x3002;</li>
<li>&#x6570;&#x636E;&#x6539;&#x53D8;&#x7684;&#x6D88;&#x606F;&#x4F20;&#x9012;&#x88AB;&#x5C4F;&#x853D;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x65E0;&#x9700;&#x624B;&#x52A8;&#x53BB;&#x5904;&#x7406;&#x72B6;&#x6001;&#x6539;&#x53D8;&#x4E8B;&#x4EF6;&#x7684;&#x53D1;&#x5E03;&#x548C;&#x8BA2;&#x9605;&#x4E86;&#xFF0C;&#x8FD9;&#x4E00;&#x5207;&#x90FD;&#x88AB;&#x5C01;&#x88C5;&#x5728;Provider&#x4E2D;&#x4E86;&#x3002;&#x8FD9;&#x771F;&#x7684;&#x5F88;&#x68D2;&#xFF0C;&#x5E2E;&#x6211;&#x4EEC;&#x7701;&#x6389;&#x4E86;&#x5927;&#x91CF;&#x7684;&#x5DE5;&#x4F5C;&#xFF01;</li>
<li>&#x5728;&#x5927;&#x578B;&#x590D;&#x6742;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x5C24;&#x5176;&#x662F;&#x9700;&#x8981;&#x5168;&#x5C40;&#x5171;&#x4EAB;&#x7684;&#x72B6;&#x6001;&#x975E;&#x5E38;&#x591A;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;Provider&#x5C06;&#x4F1A;&#x5927;&#x5927;&#x7B80;&#x5316;&#x6211;&#x4EEC;&#x7684;&#x4EE3;&#x7801;&#x903B;&#x8F91;&#xFF0C;&#x964D;&#x4F4E;&#x51FA;&#x9519;&#x7684;&#x6982;&#x7387;&#xFF0C;&#x63D0;&#x9AD8;&#x5F00;&#x53D1;&#x6548;&#x7387;&#x3002; </li>
</ol>
<h3 id="&#x4F18;&#x5316;">&#x4F18;&#x5316;</h3>
<p>&#x6211;&#x4EEC;&#x4E0A;&#x9762;&#x5B9E;&#x73B0;&#x7684;<code>ChangeNotifierProvider</code>&#x662F;&#x6709;&#x4E24;&#x4E2A;&#x660E;&#x663E;&#x7F3A;&#x70B9;&#xFF1A;&#x4EE3;&#x7801;&#x7EC4;&#x7EC7;&#x4E3A;&#x9898;&#x548C;&#x6027;&#x80FD;&#x95EE;&#x9898;&#xFF0C;&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x4E00;&#x4E00;&#x8BA8;&#x8BBA;&#x3002;</p>
<h4 id="&#x4EE3;&#x7801;&#x7EC4;&#x7EC7;&#x95EE;&#x9898;">&#x4EE3;&#x7801;&#x7EC4;&#x7EC7;&#x95EE;&#x9898;</h4>
<p>&#x6211;&#x4EEC;&#x5148;&#x770B;&#x4E00;&#x4E0B;&#x6784;&#x5EFA;&#x663E;&#x793A;&#x603B;&#x4EF7;Text&#x7684;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">var</span> cart<span class="token operator">=</span>ChangeNotifierProvider<span class="token punctuation">.</span>of<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x603B;&#x4EF7;: ${cart.totalPrice}&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x6709;&#x4E24;&#x70B9;&#x53EF;&#x4EE5;&#x4F18;&#x5316;&#xFF1A;</p>
<ol>
<li>&#x9700;&#x8981;&#x663E;&#x5F0F;&#x8C03;&#x7528;<code>ChangeNotifierProvider.of</code>&#xFF0C;&#x5F53;APP&#x5185;&#x90E8;&#x4F9D;&#x8D56;<code>CartModel</code>&#x5F88;&#x591A;&#x65F6;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x4EE3;&#x7801;&#x5C06;&#x5F88;&#x5197;&#x4F59;&#x3002;</li>
<li>&#x8BED;&#x4E49;&#x4E0D;&#x660E;&#x786E;&#xFF1B;&#x7531;&#x4E8E;<code>ChangeNotifierProvider</code>&#x662F;&#x8BA2;&#x9605;&#x8005;&#xFF0C;&#x90A3;&#x4E48;&#x4F9D;&#x8D56;<code>CartModel</code>&#x7684;Widget&#x81EA;&#x7136;&#x5C31;&#x662F;&#x8BA2;&#x9605;&#x8005;&#xFF0C;&#x5176;&#x5B9E;&#x4E5F;&#x5C31;&#x662F;&#x72B6;&#x6001;&#x7684;&#x6D88;&#x8D39;&#x8005;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x7528;<code>Builder</code> &#x6765;&#x6784;&#x5EFA;&#xFF0C;&#x8BED;&#x4E49;&#x5C31;&#x4E0D;&#x662F;&#x5F88;&#x660E;&#x786E;&#xFF1B;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x80FD;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x5177;&#x6709;&#x660E;&#x786E;&#x8BED;&#x4E49;&#x7684;Widget&#xFF0C;&#x6BD4;&#x5982;&#x5C31;&#x53EB;<code>Consumer</code>&#xFF0C;&#x8FD9;&#x6837;&#x6700;&#x7EC8;&#x7684;&#x4EE3;&#x7801;&#x8BED;&#x4E49;&#x5C06;&#x4F1A;&#x5F88;&#x660E;&#x786E;&#xFF0C;&#x53EA;&#x8981;&#x770B;&#x5230;<code>Consumer</code>&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x77E5;&#x9053;&#x5B83;&#x662F;&#x4F9D;&#x8D56;&#x67D0;&#x4E2A;&#x8DE8;&#x7EC4;&#x4EF6;&#x6216;&#x5168;&#x5C40;&#x7684;&#x72B6;&#x6001;&#x3002;</li>
</ol>
<p>&#x4E3A;&#x4E86;&#x4F18;&#x5316;&#x8FD9;&#x4E24;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C01;&#x88C5;&#x4E00;&#x4E2A;<code>Consumer</code> Widget&#xFF0C;&#x5B9E;&#x73B0;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token comment">// &#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x4FBF;&#x6377;&#x7C7B;&#xFF0C;&#x4F1A;&#x83B7;&#x5F97;&#x5F53;&#x524D;context&#x548C;&#x6307;&#x5B9A;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;Provider</span>
<span class="token keyword">class</span> <span class="token class-name">Consumer</span><span class="token operator">&lt;</span>T<span class="token operator">&gt;</span> <span class="token keyword">extends</span> <span class="token class-name">StatelessWidget</span> <span class="token punctuation">{</span>
  <span class="token function">Consumer</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    Key key<span class="token punctuation">,</span>
    <span class="token metadata symbol">@required</span> <span class="token keyword">this</span><span class="token punctuation">.</span>builder<span class="token punctuation">,</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>child<span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>  <span class="token punctuation">:</span> <span class="token keyword">assert</span><span class="token punctuation">(</span>builder <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span>key<span class="token punctuation">:</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">final</span> Widget child<span class="token punctuation">;</span>

  <span class="token keyword">final</span> Widget <span class="token function">Function</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">,</span> T value<span class="token punctuation">)</span> builder<span class="token punctuation">;</span>

  <span class="token metadata symbol">@override</span>
  Widget <span class="token function">build</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">builder</span><span class="token punctuation">(</span>
      context<span class="token punctuation">,</span>
      ChangeNotifierProvider<span class="token punctuation">.</span>of<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">//&#x81EA;&#x52A8;&#x83B7;&#x53D6;Model</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><code>Consumer</code>&#x5B9E;&#x73B0;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x5B83;&#x901A;&#x8FC7;&#x6307;&#x5B9A;&#x6A21;&#x677F;&#x53C2;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5185;&#x90E8;&#x81EA;&#x52A8;&#x8C03;&#x7528;<code>ChangeNotifierProvider.of</code>&#x83B7;&#x53D6;&#x76F8;&#x5E94;&#x7684;Model&#xFF0C;&#x5E76;&#x4E14;<code>Consumer</code>&#x8FD9;&#x4E2A;&#x540D;&#x5B57;&#x672C;&#x8EAB;&#x4E5F;&#x662F;&#x5177;&#x6709;&#x786E;&#x5207;&#x8BED;&#x4E49;&#xFF08;&#x6D88;&#x8D39;&#x8005;&#xFF09;&#x3002;&#x73B0;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5757;&#x53EF;&#x4EE5;&#x4F18;&#x5316;&#x4E3A;&#x5982;&#x4E0B;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart">Consumer<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>
  builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">,</span> cart<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x603B;&#x4EF7;: ${cart.totalPrice}&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">)</span>
</code></pre>
<p>&#x662F;&#x4E0D;&#x662F;&#x5F88;&#x4F18;&#x96C5;&#xFF01;</p>
<h4 id="&#x6027;&#x80FD;&#x95EE;&#x9898;">&#x6027;&#x80FD;&#x95EE;&#x9898;</h4>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x6027;&#x80FD;&#x95EE;&#x9898;&#xFF0C;&#x5C31;&#x5728;&#x6784;&#x5EFA;&#x201D;&#x6DFB;&#x52A0;&#x6309;&#x94AE;&#x201C;&#x7684;&#x4EE3;&#x7801;&#x5904;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;RaisedButton build&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x6784;&#x5EFA;&#x65F6;&#x8F93;&#x51FA;&#x65E5;&#x5FD7;</span>
  <span class="token keyword">return</span> <span class="token function">RaisedButton</span><span class="token punctuation">(</span>
    child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      ChangeNotifierProvider<span class="token punctuation">.</span>of<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token function">Item</span><span class="token punctuation">(</span><span class="token number">20.0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x70B9;&#x51FB;&#x201D;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&#x201C;&#x6309;&#x94AE;&#x540E;&#xFF0C;&#x7531;&#x4E8E;&#x8D2D;&#x7269;&#x8F66;&#x5546;&#x54C1;&#x603B;&#x4EF7;&#x4F1A;&#x53D8;&#x5316;&#xFF0C;&#x6240;&#x4EE5;&#x663E;&#x793A;&#x603B;&#x4EF7;&#x7684;Text&#x66F4;&#x65B0;&#x662F;&#x7B26;&#x5408;&#x9884;&#x671F;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x201D;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&#x201C;&#x6309;&#x94AE;&#x672C;&#x8EAB;&#x6CA1;&#x6709;&#x53D8;&#x5316;&#xFF0C;&#x662F;&#x4E0D;&#x5E94;&#x8BE5;&#x88AB;&#x91CD;&#x65B0;build&#x7684;&#x3002;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x8FD0;&#x884C;&#x793A;&#x4F8B;&#xFF0C;&#x6BCF;&#x6B21;&#x70B9;&#x51FB;&#x201D;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&#x201C;&#x6309;&#x94AE;&#xFF0C;&#x63A7;&#x5236;&#x53F0;&#x90FD;&#x4F1A;&#x8F93;&#x51FA;&quot;RaisedButton build&quot;&#x65E5;&#x5FD7;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x201D;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&#x201C;&#x6309;&#x94AE;&#x5728;&#x6BCF;&#x6B21;&#x70B9;&#x51FB;&#x65F6;&#x5176;&#x81EA;&#x8EAB;&#x90FD;&#x4F1A;&#x91CD;&#x65B0;build&#xFF01;&#x8FD9;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;&#x5982;&#x679C;&#x4F60;&#x5DF2;&#x7ECF;&#x7406;&#x89E3;&#x4E86;<code>InheritedWidget</code>&#x7684;&#x66F4;&#x65B0;&#x673A;&#x5236;&#xFF0C;&#x90A3;&#x4E48;&#x7B54;&#x6848;&#x4E00;&#x773C;&#x5C31;&#x80FD;&#x770B;&#x51FA;&#xFF1A;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x6784;&#x5EFA;<code>RaisedButton</code>&#x7684;<code>Builder</code>&#x4E2D;&#x8C03;&#x7528;&#x4E86;<code>ChangeNotifierProvider.of</code>&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4F9D;&#x8D56;&#x4E86;Widget&#x6811;&#x4E0A;&#x9762;&#x7684;<code>InheritedWidget</code>&#xFF08;&#x5373;<code>InheritedProvider</code> &#xFF09;Widget&#xFF0C;&#x6240;&#x4EE5;&#x5F53;&#x6DFB;&#x52A0;&#x5B8C;&#x5546;&#x54C1;&#x540E;&#xFF0C;<code>CartModel</code>&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x4F1A;&#x901A;&#x77E5;<code>ChangeNotifierProvider</code>, &#x800C;<code>ChangeNotifierProvider</code>&#x5219;&#x4F1A;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x5B50;&#x6811;&#xFF0C;&#x6240;&#x4EE5;<code>InheritedProvider</code>&#x5C06;&#x4F1A;&#x66F4;&#x65B0;&#xFF0C;&#x6B64;&#x65F6;&#x4F9D;&#x8D56;&#x5B83;&#x7684;&#x5B50;&#x5B59;Widget&#x5C31;&#x4F1A;&#x88AB;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x3002;</p>
<p>&#x95EE;&#x9898;&#x7684;&#x539F;&#x56E0;&#x641E;&#x6E05;&#x695A;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x907F;&#x514D;&#x8FD9;&#x4E0D;&#x5FC5;&#x8981;&#x91CD;&#x6784;&#x5462;&#xFF1F;&#x65E2;&#x7136;&#x6309;&#x94AE;&#x91CD;&#x65B0;&#x88AB;build&#x662F;&#x56E0;&#x4E3A;&#x6309;&#x94AE;&#x548C;<code>InheritedWidget</code>&#x5EFA;&#x7ACB;&#x4E86;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x53EA;&#x8981;&#x6253;&#x7834;&#x6216;&#x89E3;&#x9664;&#x8FD9;&#x79CD;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;&#x90A3;&#x4E48;&#x5982;&#x4F55;&#x89E3;&#x9664;&#x6309;&#x94AE;&#x548C;<code>InheritedWidget</code>&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x4E0A;&#x4E00;&#x8282;&#x4ECB;&#x7ECD;<code>InheritedWidget</code>&#x65F6;&#x5DF2;&#x7ECF;&#x8BB2;&#x8FC7;&#x4E86;&#xFF1A;&#x8C03;&#x7528;<code>inheritFromWidgetOfExactType()</code> &#x548C; <code>ancestorInheritedElementForWidgetOfExactType()</code>&#x7684;&#x533A;&#x522B;&#x5C31;&#x662F;&#x524D;&#x8005;&#x4F1A;&#x6CE8;&#x518C;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x800C;&#x540E;&#x8005;&#x4E0D;&#x4F1A;&#x3002;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x5C06;<code>ChangeNotifierProvider.of</code>&#x7684;&#x5B9E;&#x73B0;&#x6539;&#x4E3A;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x65E2;&#x53EF;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"> <span class="token comment">//&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;listen&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x662F;&#x5426;&#x5EFA;&#x7ACB;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;</span>
  <span class="token keyword">static</span> T of<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">(</span>BuildContext context<span class="token punctuation">,</span> <span class="token punctuation">{</span>bool listen <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">final</span> type <span class="token operator">=</span> _typeOf<span class="token operator">&lt;</span>InheritedProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">final</span> provider <span class="token operator">=</span> listen
        <span class="token operator">?</span> context<span class="token punctuation">.</span><span class="token function">inheritFromWidgetOfExactType</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span> <span class="token operator">as</span> InheritedProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span>
        <span class="token punctuation">:</span> context<span class="token punctuation">.</span><span class="token function">ancestorInheritedElementForWidgetOfExactType</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">.</span>widget
            <span class="token operator">as</span> InheritedProvider<span class="token operator">&lt;</span>T<span class="token operator">&gt;</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> provider<span class="token punctuation">.</span>data<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x5C06;&#x8C03;&#x7528;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x6539;&#x4E3A;&#xFF1A;</p>
<pre class="language-"><code class="lang-dart"><span class="token function">Column</span><span class="token punctuation">(</span>
    children<span class="token punctuation">:</span> <span class="token operator">&lt;</span>Widget<span class="token operator">&gt;</span><span class="token punctuation">[</span>
      Consumer<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>
        builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>BuildContext context<span class="token punctuation">,</span> cart<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span><span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x603B;&#x4EF7;: ${cart.totalPrice}&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token function">Builder</span><span class="token punctuation">(</span>builder<span class="token punctuation">:</span> <span class="token punctuation">(</span>context<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;RaisedButton build&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token function">RaisedButton</span><span class="token punctuation">(</span>
          child<span class="token punctuation">:</span> <span class="token function">Text</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
          onPressed<span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// listen &#x8BBE;&#x4E3A;false&#xFF0C;&#x4E0D;&#x5EFA;&#x7ACB;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;</span>
            ChangeNotifierProvider<span class="token punctuation">.</span>of<span class="token operator">&lt;</span>CartModel<span class="token operator">&gt;</span><span class="token punctuation">(</span>context<span class="token punctuation">,</span> listen<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token function">Item</span><span class="token punctuation">(</span><span class="token number">20.0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">)</span>
</code></pre>
<p>&#x4FEE;&#x6539;&#x540E;&#x5728;&#x6B64;&#x8FD0;&#x884C;&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x53D1;&#x73B0;&#x70B9;&#x51FB;&#x201D;&#x6DFB;&#x52A0;&#x5546;&#x54C1;&#x201C;&#x6309;&#x94AE;&#x540E;&#xFF0C;&#x63A7;&#x5236;&#x53F0;&#x4E0D;&#x4F1A;&#x518D;&#x8F93;&#x51FA;&quot;RaisedButton build&quot;&#x4E86;&#xFF0C;&#x5373;&#x6309;&#x94AE;&#x4E0D;&#x4F1A;&#x88AB;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x4E86;&#x3002;&#x800C;&#x603B;&#x4EF7;&#x4ECD;&#x7136;&#x4F1A;&#x66F4;&#x65B0;&#xFF0C;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;<code>Consumer</code>&#x4E2D;&#x8C03;&#x7528;<code>ChangeNotifierProvider.of</code>&#x65F6;<code>listen</code>&#x503C;&#x4E3A;&#x9ED8;&#x8BA4;&#x503C;true&#xFF0C;&#x6240;&#x4EE5;&#x8FD8;&#x662F;&#x4F1A;&#x5EFA;&#x7ACB;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x3002;</p>
<p>&#x81F3;&#x6B64;&#x6211;&#x4EEC;&#x4FBF;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x8FF7;&#x4F60;&#x7684;Provider&#xFF0C;&#x5B83;&#x5177;&#x5907;Pub&#x4E0A;Provider Package&#x4E2D;&#x7684;&#x6838;&#x5FC3;&#x529F;&#x80FD;&#xFF1B;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x8FF7;&#x4F60;&#x7248;&#x529F;&#x80FD;&#x5E76;&#x4E0D;&#x5168;&#x9762;&#xFF0C;&#x5982;&#x53EA;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x53EF;&#x76D1;&#x542C;&#x7684;ChangeNotifierProvider&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x53EA;&#x7528;&#x4E8E;&#x6570;&#x636E;&#x5171;&#x4EAB;&#x7684;Provider&#xFF1B;&#x53E6;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x5B9E;&#x73B0;&#x6709;&#x4E9B;&#x8FB9;&#x754C;&#x4E5F;&#x6CA1;&#x6709;&#x8003;&#x8651;&#x7684;&#x5230;&#xFF0C;&#x6BD4;&#x5982;&#x5982;&#x4F55;&#x4FDD;&#x8BC1;&#x5728;Widget&#x6811;&#x91CD;&#x65B0;build&#x65F6;Model&#x59CB;&#x7EC8;&#x662F;&#x5355;&#x4F8B;&#x7B49;&#x3002;&#x6240;&#x4EE5;&#x5EFA;&#x8BAE;&#x8BFB;&#x8005;&#x5728;&#x5B9E;&#x6218;&#x4E2D;&#x8FD8;&#x662F;&#x4F7F;&#x7528;Provider Package&#xFF0C;&#x800C;&#x672C;&#x8282;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x8FF7;&#x4F60;Provider&#x7684;&#x4E3B;&#x8981;&#x76EE;&#x7684;&#x4E3B;&#x8981;&#x662F;&#x4E3A;&#x4E86;&#x5E2E;&#x52A9;&#x8BFB;&#x8005;&#x4E86;&#x89E3;Provider Package&#x5E95;&#x5C42;&#x7684;&#x539F;&#x7406;&#x3002;</p>
<h3 id="&#x5176;&#x5B83;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x5305;">&#x5176;&#x5B83;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x5305;</h3>
<p>&#x73B0;&#x5728;Flutter&#x793E;&#x533A;&#x5DF2;&#x7ECF;&#x6709;&#x5F88;&#x591A;&#x4E13;&#x95E8;&#x7528;&#x4E8E;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x7684;&#x5305;&#x4E86;&#xFF0C;&#x5728;&#x6B64;&#x6211;&#x4EEC;&#x5217;&#x51FA;&#x51E0;&#x4E2A;&#x76F8;&#x5BF9;&#x8BC4;&#x5206;&#x6BD4;&#x8F83;&#x9AD8;&#x7684;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x5305;&#x540D;</th>
<th>&#x4ECB;&#x7ECD;</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://pub.flutter-io.cn/packages/provider" target="_blank">Provider</a> &amp; <a href="https://pub.flutter-io.cn/packages/scoped_model" target="_blank">Scoped Model</a></td>
<td>&#x8FD9;&#x4E24;&#x4E2A;&#x5305;&#x90FD;&#x662F;&#x57FA;&#x4E8E;<code>InheritedWidget</code>&#x7684;&#xFF0C;&#x539F;&#x7406;&#x76F8;&#x4F3C;</td>
</tr>
<tr>
<td><a href="https://pub.flutter-io.cn/packages/flutter_redux" target="_blank">Redux</a></td>
<td>&#x662F;Web&#x5F00;&#x53D1;&#x4E2D;React&#x751F;&#x6001;&#x94FE;&#x4E2D;Redux&#x5305;&#x7684;Flutter&#x5B9E;&#x73B0;</td>
</tr>
<tr>
<td><a href="https://pub.dev/packages/flutter_mobx" target="_blank">MobX</a></td>
<td>&#x662F;Web&#x5F00;&#x53D1;&#x4E2D;React&#x751F;&#x6001;&#x94FE;&#x4E2D;MobX&#x5305;&#x7684;Flutter&#x5B9E;&#x73B0;</td>
</tr>
<tr>
<td><a href="https://pub.dev/packages/flutter_bloc" target="_blank">BLoC</a></td>
<td>&#x662F;BLoC&#x6A21;&#x5F0F;&#x7684;Flutter&#x5B9E;&#x73B0;</td>
</tr>
</tbody>
</table>
<p>&#x5728;&#x6B64;&#x7B14;&#x8005;&#x4E0D;&#x5BF9;&#x8FD9;&#x4E9B;&#x5305;&#x505A;&#x63A8;&#x8350;&#xFF0C;&#x8BFB;&#x8005;&#x6709;&#x5174;&#x8DA3;&#x90FD;&#x53EF;&#x4EE5;&#x7814;&#x7A76;&#x4E00;&#x4E0B;&#xFF0C;&#x4E86;&#x89E3;&#x5B83;&#x4EEC;&#x5404;&#x81EA;&#x7684;&#x601D;&#x60F3;&#x3002;</p>
<h3 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h3>
<p>&#x672C;&#x8282;&#x901A;&#x8FC7;&#x4ECB;&#x7ECD;&#x4E8B;&#x4EF6;&#x603B;&#x7EBF;&#x5728;&#x8DE8;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#x4E2D;&#x7684;&#x4E00;&#x4E9B;&#x7F3A;&#x70B9;&#x5F15;&#x51FA;&#x4E86;&#x901A;&#x8FC7;<code>InheritedWidget</code>&#x6765;&#x5B9E;&#x73B0;&#x72B6;&#x6001;&#x7684;&#x5171;&#x4EAB;&#x7684;&#x601D;&#x60F3;&#xFF0C;&#x7136;&#x540E;&#x57FA;&#x4E8E;&#x8BE5;&#x601D;&#x60F3;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;Provider&#xFF0C;&#x5728;&#x5B9E;&#x73B0;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E5F;&#x66F4;&#x6DF1;&#x5165;&#x7684;&#x63A2;&#x7D22;&#x4E86;<code>InheritedWidget</code>&#x4E0E;&#x5176;&#x4F9D;&#x8D56;&#x9879;&#x7684;&#x6CE8;&#x518C;&#x673A;&#x5236;&#x548C;&#x66F4;&#x65B0;&#x673A;&#x5236;&#x3002;&#x901A;&#x8FC7;&#x672C;&#x8282;&#x7684;&#x5B66;&#x4E60;&#xFF0C;&#x8BFB;&#x8005;&#x5E94;&#x8BE5;&#x8FBE;&#x5230;&#x4E24;&#x4E2A;&#x76EE;&#x6807;&#xFF0C;&#x9996;&#x5148;&#x662F;&#x5BF9;<code>InheritedWidget</code>&#x5F7B;&#x5E95;&#x5403;&#x900F;&#xFF0C;&#x5176;&#x6B21;&#x662F;Provider&#x7684;&#x8BBE;&#x8BA1;&#x601D;&#x60F3;&#x3002;</p>
<p><code>InheritedWidget</code>&#x662F;Flutter&#x4E2D;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x4E2A;Widget&#xFF0C;&#x50CF;&#x56FD;&#x9645;&#x5316;&#x3001;&#x4E3B;&#x9898;&#x7B49;&#x90FD;&#x662F;&#x901A;&#x8FC7;&#x5B83;&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x4E5F;&#x4E0D;&#x60DC;&#x7BC7;&#x5E45;&#xFF0C;&#x901A;&#x8FC7;&#x597D;&#x51E0;&#x8282;&#x6765;&#x4ECB;&#x7ECD;&#x5B83;&#x7684;&#xFF0C;&#x5728;&#x4E0B;&#x4E00;&#x8282;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4ECB;&#x7ECD;&#x53E6;&#x4E00;&#x4E2A;&#x57FA;&#x4E8E;<code>InheritedWidget</code>&#x7684;&#x7EC4;&#x4EF6;Theme(&#x4E3B;&#x9898;)&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="inherited_widget.html" class="navigation navigation-prev " aria-label="Previous page: 7.2：数据共享（InheritedWidget）">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="theme.html" class="navigation navigation-next " aria-label="Next page: 7.4：颜色和主题（Theme）">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"7.3： 跨组件状态共享（Provider）","level":"2.7.3","depth":2,"next":{"title":"7.4：颜色和主题（Theme）","level":"2.7.4","depth":2,"path":"chapter7/theme.md","ref":"chapter7/theme.md","articles":[]},"previous":{"title":"7.2：数据共享（InheritedWidget）","level":"2.7.2","depth":2,"path":"chapter7/inherited_widget.md","ref":"chapter7/inherited_widget.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","search-pro","ace","splitter","prism","donate","-highlight"],"root":"docs","styles":{"website":"styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"prism":{"lang":{"shell":"bash"}},"ace":{},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["Gitbook Use"]},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action@1.0.3/docs/imgs/pay.jpeg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"wendux 824783146@qq.com","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《Flutter实战》","language":"zh-hans","links":{"sharing":{"all":null,"facebook":null,"google":null,"twitter":null,"weibo":null},"sidebar":{"<div><img height=20 style='position:relative; top:5px; padding-right:5px ' src='https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/favicon.png'/> Flutter中文网</div>":"https://flutterchina.club"}},"gitbook":"*","description":"记录Gitbook的配置和一些插件的使用"},"file":{"path":"chapter7/provider.md","mtime":"2019-08-22T07:11:05.748Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-08-22T09:03:18.661Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>



    <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/theme.js"></script>
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-ace/ace/ace.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-ace/ace.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="https://cdn.jsdelivr.net/gh/flutterchina/flutter-in-action/_book/gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    


<script>
    var _hmt = _hmt || [];
    $("#bd").remove();
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?170231fea4f81697eb046edc1a91fe5b";
        var s = document.getElementsByTagName("script")[0];
        hm.id = "bd"
        s.parentNode.insertBefore(hm, s);
    })();

    function init() {
        var p = location.pathname;
        _hmt.push(['_trackPageview', p]);
        if (p[p.length - 1] === '/') {
            p += "index.md"
        } else {
            p = p.split(".")[0] + ".md";
        }
        p = "https://github.com/flutterchina/flutter-in-action/blob/master/docs" + p;
        $(".pull-right .fa-edit").parent("a").attr("href", p);
        $("table").wrap("<div style='overflow: auto'></div>");
        //百度统计
        var e = /([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi, r = window.location.href,
            t = document.referrer;
        if (!e.test(r)) {
            var o = "https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif";
            t ? (o += "?r=" + encodeURIComponent(document.referrer), r && (o += "&l=" + r)) : r && (o += "?l=" + r);
            var i = new Image;
            i.src = o
        }
        $(".copyright").remove();
        $("<div class='copyright'> 本书仍在勘误阶段，请勿以任何形式私自传播，作者保留对本书的版权。</div>").appendTo(".page-inner");
    }

    function hookAPI(api, ob, fn) {
        return function () {
            var result = api.apply(ob, [].slice.call(arguments));
            setTimeout(fn, 1000);
            return result;
        }
    }

    function addAD() {
        if (location.href !== 'https://book.flutterchina.club/' && $("#book-search-results .ad").length == 0) {
            var id=Math.ceil(Math.random()*10)%2;
            $(".ad"+id).clone().hide().fadeIn().prependTo("#book-search-results")
            setTimeout(function () {
                $(".body-inner").animate({scrollTop: 0}, 200)
            }, 800);
        }
    }

    function _track(p,url) {
        _hmt.push(['_trackEvent', 'ad', 'click', p]);
        setTimeout(function () {
            location.href = url
        }, 100);
    }

    // setInterval(addAD, 300);

    init();

    if (history.pushState) {
        history.pushState = hookAPI(history.pushState, history, init);
    }

</script>
</body>

</html>

